﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using PointOfSale.Global;
using PointOfSale.Transaction;
using PointOfSale.PointOfSale;
using PointOfSale.Class;

namespace PointOfSale.Transaction
{
	/// <summary>
	/// Summary description for POSPurchaseBillList.
	/// </summary>
	public class frmPOSPurchaseBillList : System.Windows.Forms.Form
	{
		
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.LookupComboBox cboCategory;
		private System.Windows.Forms.Label lblCategory;
		private System.Windows.Forms.Label lblDate;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button btnChangeDetail;
		private System.Windows.Forms.TextBox txtSoHoaDon;
		private System.Windows.Forms.Label lblSoHoaDon;
		private System.Windows.Forms.ColumnHeader colPurchaseId;
		private System.Windows.Forms.ColumnHeader colBillDate;
		private System.Windows.Forms.ColumnHeader colTotal;
		private System.Windows.Forms.ColumnHeader colTax;
		private System.Windows.Forms.ColumnHeader colDiscount;
		private System.Windows.Forms.ColumnHeader colBillAmount;
		private System.Windows.Forms.DateTimePicker dtpngay;
		private ListView.SelectedListViewItemCollection selectedRow = null;

		public frmPOSPurchaseBillList()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.dtpngay = new System.Windows.Forms.DateTimePicker();
			this.lblDate = new System.Windows.Forms.Label();
			this.lblCategory = new System.Windows.Forms.Label();
			this.cboCategory = new System.Windows.Forms.LookupComboBox();
			this.lblSoHoaDon = new System.Windows.Forms.Label();
			this.txtSoHoaDon = new System.Windows.Forms.TextBox();
			this.btnClose = new System.Windows.Forms.Button();
			this.listView1 = new System.Windows.Forms.ListView();
			this.colPurchaseId = new System.Windows.Forms.ColumnHeader();
			this.colBillDate = new System.Windows.Forms.ColumnHeader();
			this.colTotal = new System.Windows.Forms.ColumnHeader();
			this.colTax = new System.Windows.Forms.ColumnHeader();
			this.colDiscount = new System.Windows.Forms.ColumnHeader();
			this.colBillAmount = new System.Windows.Forms.ColumnHeader();
			this.btnChangeDetail = new System.Windows.Forms.Button();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.dtpngay);
			this.groupBox2.Controls.Add(this.lblDate);
			this.groupBox2.Controls.Add(this.lblCategory);
			this.groupBox2.Controls.Add(this.cboCategory);
			this.groupBox2.Controls.Add(this.lblSoHoaDon);
			this.groupBox2.Controls.Add(this.txtSoHoaDon);
			this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox2.Location = new System.Drawing.Point(16, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(576, 48);
			this.groupBox2.TabIndex = 1;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Điều kiện tìm kiếm";
			// 
			// dtpngay
			// 
			this.dtpngay.Location = new System.Drawing.Point(256, 16);
			this.dtpngay.Name = "dtpngay";
			this.dtpngay.Size = new System.Drawing.Size(88, 20);
			this.dtpngay.TabIndex = 6;
			this.dtpngay.ValueChanged += new System.EventHandler(this.dtpngay_ValueChanged);
			// 
			// lblDate
			// 
			this.lblDate.Location = new System.Drawing.Point(200, 20);
			this.lblDate.Name = "lblDate";
			this.lblDate.Size = new System.Drawing.Size(48, 16);
			this.lblDate.TabIndex = 5;
			this.lblDate.Text = "Ngày";
			// 
			// lblCategory
			// 
			this.lblCategory.Location = new System.Drawing.Point(8, 18);
			this.lblCategory.Name = "lblCategory";
			this.lblCategory.Size = new System.Drawing.Size(72, 16);
			this.lblCategory.TabIndex = 4;
			this.lblCategory.Text = "Chủng loại";
			// 
			// cboCategory
			// 
			this.cboCategory.AllowTypeAllSymbols = true;
			this.cboCategory.Location = new System.Drawing.Point(80, 16);
			this.cboCategory.Name = "cboCategory";
			this.cboCategory.Size = new System.Drawing.Size(112, 21);
			this.cboCategory.TabIndex = 0;
			this.cboCategory.SelectedIndexChanged += new System.EventHandler(this.cboCategory_SelectedIndexChanged);
			// 
			// lblSoHoaDon
			// 
			this.lblSoHoaDon.AutoSize = true;
			this.lblSoHoaDon.Location = new System.Drawing.Point(368, 16);
			this.lblSoHoaDon.Name = "lblSoHoaDon";
			this.lblSoHoaDon.Size = new System.Drawing.Size(62, 16);
			this.lblSoHoaDon.TabIndex = 13;
			this.lblSoHoaDon.Text = "Số hóa đơn";
			// 
			// txtSoHoaDon
			// 
			this.txtSoHoaDon.Location = new System.Drawing.Point(432, 16);
			this.txtSoHoaDon.Name = "txtSoHoaDon";
			this.txtSoHoaDon.Size = new System.Drawing.Size(80, 20);
			this.txtSoHoaDon.TabIndex = 12;
			this.txtSoHoaDon.Text = "";
			this.txtSoHoaDon.TextChanged += new System.EventHandler(this.txtSoHoaDon_TextChanged);
			// 
			// btnClose
			// 
			this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnClose.Location = new System.Drawing.Point(536, 488);
			this.btnClose.Name = "btnClose";
			this.btnClose.TabIndex = 6;
			this.btnClose.Text = "Thoát";
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// listView1
			// 
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.colPurchaseId,
																						this.colBillDate,
																						this.colTotal,
																						this.colTax,
																						this.colDiscount,
																						this.colBillAmount});
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.Location = new System.Drawing.Point(8, 64);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(600, 416);
			this.listView1.TabIndex = 9;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
			// 
			// colPurchaseId
			// 
			this.colPurchaseId.Text = "Số hóa đơn";
			this.colPurchaseId.Width = 78;
			// 
			// colBillDate
			// 
			this.colBillDate.Text = "Ngày lập";
			this.colBillDate.Width = 134;
			// 
			// colTotal
			// 
			this.colTotal.Text = "Tổng số tiền";
			this.colTotal.Width = 94;
			// 
			// colTax
			// 
			this.colTax.Text = "Thuế(%)";
			this.colTax.Width = 58;
			// 
			// colDiscount
			// 
			this.colDiscount.Text = "Giảm giá(%)";
			this.colDiscount.Width = 73;
			// 
			// colBillAmount
			// 
			this.colBillAmount.Text = "Tổng hóa đơn";
			this.colBillAmount.Width = 159;
			// 
			// btnChangeDetail
			// 
			this.btnChangeDetail.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnChangeDetail.Location = new System.Drawing.Point(440, 488);
			this.btnChangeDetail.Name = "btnChangeDetail";
			this.btnChangeDetail.Size = new System.Drawing.Size(88, 24);
			this.btnChangeDetail.TabIndex = 10;
			this.btnChangeDetail.Text = "Sửa hóa đơn";
			this.btnChangeDetail.Click += new System.EventHandler(this.btnChangeDetail_Click);
			// 
			// frmPOSPurchaseBillList
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(614, 515);
			this.Controls.Add(this.btnChangeDetail);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.groupBox2);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmPOSPurchaseBillList";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Tìm kiếm hóa đơn mua hàng";
			this.Load += new System.EventHandler(this.frmPOSPurchaseBillList_Load);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/*
		 * Ham lay cac item category tu co so du lieu dua vao cboCategory
		 */
		private void loadItemCategoryToCboCategory(){
			string SelectCommand = "select CategoryName from tblCategory";
			SqlCommand _sqlCommand = new SqlCommand(SelectCommand,clsGlobal.g_DataConn);
			SqlDataReader dr = _sqlCommand.ExecuteReader();
			this.cboCategory.Items.Add("");
			while(dr.Read())
			{
				this.cboCategory.Items.Add(dr["CategoryName"].ToString());
			}
			dr.Close();			
		}

		private void frmPOSPurchaseBillList_Load(object sender, System.EventArgs e)
		{
			
			loadItemCategoryToCboCategory();
			if(this.cboCategory.Items.Count > 0){
				this.cboCategory.SelectedIndex = 0;
			}
			this.txtSoHoaDon.Focus();
			
			this.CancelButton = this.btnClose;
			hienlistview();
		}

		
		private void hienlistview()
		{
			//Xoa cac item hien co trong listview
			foreach(ListViewItem item in this.listView1.Items){
				this.listView1.Items.Remove(item);
			}

			//Thanh lap truy van chon
			string strSql = "select a.purchaseId, a.billDate, a.total, a.tax, a.discount, "
					+ " a.billAmount, a.departmentId "
					+ " from tblPurchase a  where datediff(day,billdate,'"+dtpngay.Value.ToString("MMM dd yyyy")+"') = 0 ";
		
			if (this.cboCategory.Text.Length != 0)
			{
					string strCategoryID = null;
					string SelectCommand = "select categoryID from tblCategory where CategoryName = '" + this.cboCategory.Text + "'";
					SqlCommand _sqlCommand = new SqlCommand(SelectCommand,clsGlobal.g_DataConn);
					SqlDataReader dr = _sqlCommand.ExecuteReader();
					while(dr.Read())
					{
						strCategoryID = dr ["CategoryID"].ToString();
					}
					dr.Close();

					strSql += "and ";
					strSql += "purchaseID in ";
					strSql += "(select purchaseID from tblPurchaseDetail where ItemID in ";
					strSql += "(select ItemID from tblItem where CategoryID = " + strCategoryID + "))";            
			
			}
			if (this.txtSoHoaDon.Text.Length > 0) 
			{
				strSql += " and purchaseId = " + this.txtSoHoaDon.Text;
			}
			strSql += " order by billDate desc";
			
			//Truy cap co so du lieu, dua du lieu vao listview
			DataTable dt = clsPurchase.GetDanhSachHoaDon(strSql);
			for(int i = 0; i < dt.Rows.Count; i++)
			{
				if(Convert.ToInt32(dt.Rows[i]["departmentId"]) == clsGlobal.g_DepartmentId)
				{
					ListViewItem lvi = new ListViewItem(new string[]{dt.Rows[i]["purchaseId"].ToString(), 
																		dt.Rows[i]["billDate"].ToString(), dt.Rows[i]["total"].ToString(), 
																		dt.Rows[i]["tax"].ToString(), dt.Rows[i]["discount"].ToString(), 
																		dt.Rows[i]["billAmount"].ToString()});
					this.listView1.Items.Add(lvi);
				}
			}
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnChangeDetail_Click(object sender, System.EventArgs e)
		{
			int purchaseID = Convert.ToInt32(this.listView1.FocusedItem.SubItems[0].Text);
			frmEditPurchaseBill _frmEditPurchaseBill = new frmEditPurchaseBill(purchaseID);
			_frmEditPurchaseBill.Show();	
			
		}

		private void cboCategory_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		hienlistview();
		}

		private void dtpngay_ValueChanged(object sender, System.EventArgs e)
		{
			hienlistview();
		}

		private void txtSoHoaDon_TextChanged(object sender, System.EventArgs e)
		{
			hienlistview();
		}

		private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}
